package com.uchedao.seller.ui.car.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchedao.seller.R;
import com.uchedao.seller.http.entity.CarBiddingBean;
import com.uchedao.seller.ui.adapter.CommonAdapter;
import com.uchedao.seller.ui.adapter.ViewHolder;
import com.uchedao.seller.ui.car.manage.ViewBidActivity;
import com.uchedao.seller.util.Arith;
import com.uchedao.seller.widget.dialog.CutPriceDialog;

import java.util.List;

/**
 * Created by hang on 2015/10/23.
 */
public class CarBiddingAdapter extends CommonAdapter<CarBiddingBean> {

    public int biddingCount; //正在竞拍车辆数
    public int bidEndCount;  // 已出价车辆数

    public CarBiddingAdapter(Context context, List<CarBiddingBean> data) {
        super(context, data, R.layout.item_car_bidding);
    }

    @Override
    public void conver(ViewHolder holder, final CarBiddingBean item, int position) {
        holder.getView(R.id.btnSubmit).setVisibility(View.GONE);

        Button btnPromote = holder.getView(R.id.btnPromote);
        Button btnViewPrice = holder.getView(R.id.btnViewPrice);
        Button btnDelete = holder.getView(R.id.btnDelete);
        Button btnUpdate = holder.getView(R.id.btnUpdate);
        Button btnCutPrice = holder.getView(R.id.btnCutPrice);

        btnViewPrice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(mContext, ViewBidActivity.class);
                it.putExtra("data", item);
                it.putExtra("click", true);
                mContext.startActivity(it);
            }
        });
        btnCutPrice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CutPriceDialog dlg = new CutPriceDialog(mContext);
                dlg.setCarBiddingBean(item);
                dlg.show();
            }
        });

        TextView tvTag = holder.getView(R.id.tvCarItemTag);
        LinearLayout llContent = holder.getView(R.id.llCarItemContent);
        LinearLayout llBtns = holder.getView(R.id.llCarItemBtns);
        LinearLayout llSecret = holder.getView(R.id.llCarItemSecret);

        if(item.getStatus() == -1) {
            // 显示竞价中header
            tvTag.setVisibility(View.VISIBLE);
            llContent.setVisibility(View.GONE);
            llBtns.setVisibility(View.GONE);
            llSecret.setVisibility(View.GONE);
            tvTag.setText(biddingCount+"辆车正在竞价，已出价"+bidEndCount+"台。");
        } else if(item.getStatus() == -2) {
            // 显示竞价结束header
            tvTag.setVisibility(View.VISIBLE);
            llContent.setVisibility(View.GONE);
            llBtns.setVisibility(View.GONE);
            llSecret.setVisibility(View.GONE);
            tvTag.setText("本日竞价结束的车辆。");
        } else if(item.getStatus() == 1) {
            // 显示竞价中车辆
            tvTag.setVisibility(View.GONE);
            llContent.setVisibility(View.VISIBLE);
            llBtns.setVisibility(View.VISIBLE);
            llSecret.setVisibility(View.GONE);

            btnPromote.setVisibility(View.VISIBLE);
            btnViewPrice.setVisibility(View.VISIBLE);
            btnDelete.setVisibility(View.GONE);
            btnUpdate.setVisibility(View.GONE);
            btnCutPrice.setVisibility(View.GONE);

            initContent(holder, item, position);
        } else if(item.getStatus()==4 || item.getStatus()==7) {
            // 显示竞价结束车辆
            tvTag.setVisibility(View.GONE);
            llContent.setVisibility(View.VISIBLE);
            llBtns.setVisibility(View.VISIBLE);
            llSecret.setVisibility(View.VISIBLE);

            btnDelete.setVisibility(View.VISIBLE);
            btnUpdate.setVisibility(View.VISIBLE);
            btnCutPrice.setVisibility(View.VISIBLE);
            btnPromote.setVisibility(View.GONE);
            if(item.getBid_num() > 0) {
                btnViewPrice.setVisibility(View.VISIBLE);
                holder.setText(R.id.tvCarItemSecretDesc, R.string.bid_secret_somebody);
            } else {
                btnViewPrice.setVisibility(View.GONE);
                holder.setText(R.id.tvCarItemSecretDesc, R.string.bid_secret_nobody);
            }
            initContent(holder, item, position);
        }
    }

    /**
     * 填充车辆内容
     */
    public void initContent(ViewHolder holder, CarBiddingBean item, int position) {
        holder.setImageByURL(R.id.ivCarItemPic, item.getCover_photo());
        holder.setText(R.id.tvCarItemTitle, item.getTitle());
        holder.setText(R.id.tvCarItemKmDate, Arith.div(item.getKilometer(),10000)+"万公里 "+item.getCard_time());
        holder.setText(R.id.tvCartItemReservePrice, (Arith.div(item.getReserve_price(),10000)+""));
        holder.setText(R.id.tvCarItemMaxPrice, item.getBid_num()>0? Arith.div(item.getMax_bid_price(),10000)+"":"--");
        holder.setText(R.id.tvCarItemEndTime, item.getEnd_time()+"结束");
        holder.setText(R.id.tvCarItemLookNum, item.getLook_num()+"人围观");
        holder.setText(R.id.tvCarItemBidNum, item.getBid_num()+"人出价");
    }
}
